<template>
  <div class="set-box">  
    <group>
        <popup-picker title="变更类型" v-model="type" show-name :data="typeList" placeholder="请选择变更类型"></popup-picker>
    </group> 
    <row :title="rowTitle" v-if="type[0]">
      <input class="ipt" type="number" slot="rowRight" :placeholder="placeHodler" v-model="stockNum">
    </row>
    <div class="reson invoice-photo">
        <div class="reson-title">原因</div>
        <div class="reson-box">
            <textarea  v-model="reson" :placeholder="'请输入库存变更原因'"  class="areatext"></textarea>
        </div>
    </div>
    <div class="invoice-photo">
        <div class="map-title">图片</div>
        <!-- 上传图片 start --> 
        <div class="wrap-box">
            <div class="wrpa-item" v-for="(item,index) in pictrueList" :key="index">
                <div class="warp-img-box" >
                    <img    class="warp-img" :src="item"  alt="" />
                    <div class="iconfont iconguanbi " @click="delThis(index)"></div>
                </div>
            </div>
            <div class="uploader-box" v-if="pictrueList.length<9">
                <div class="add" @click="chooseType()">
                    <div class="add-image" align="center">
                        <i class="iconfont iconxiangji"></i>
                        <p class="font13">添加图片</p>
                    </div>
                </div>
            </div>  
        </div>
        <!-- 上传图片 end --> 
        <input @change="fileChange($event)" type="file" ref="inputer" id="upload_file" multiple style="display: none"/>
        <div class="height120"></div>
        <div class="btn isbtn" @click="submit()">提交</div>
    </div>
  </div>
</template>

<script>

import row from '@/components/row'
import api from '@/api/api.js' 
import { Group, PopupPicker } from 'vux'
export default { 
  components: {
      row,
      Group,
      PopupPicker
  },
  data () {
    return {
      stockNum:'',
      reson:'',
      pictrueList:[],
      type:[],
      typeList:[[{
            name: '损耗',
            value: '1',
          },
          {
            name: '丢失',
            value: '2',
          },
          {
            name: '增加',
            value: '3',
          }
      ]]
    }
  },
  computed:{
      placeHodler(){
          if(! this.type[0]){  return '请输入变更数量' }
          let tip = this.type[0] == 1 ? '损耗': this.type[0] == 2 ? '丢失' : '增加';
          return '请输入'+tip+'个数'
      },
      rowTitle(){
          if(! this.type[0]){  return '变更数量' }
          let tip = this.type[0] == 1 ? '损耗': this.type[0] == 2 ? '丢失' : '增加';
          return tip+'数量'
      }
  },
   watch:{ 
    type:{
      handler:(val)=>{
        console.log(val)
      }
    }
   },
  mounted () {
  },
  filters: {
     
  },
  methods: {
     //上传照片
    chooseType() { 
        document.getElementById('upload_file').click();
    },
    fileChange(el) {
        let file = this.$refs.inputer.files; 
        api.publick.uploadFilePost({files:file},
            res=>{
                if(res.data.resultCode==0){  
                    this.pictrueList.push(res.data.result); 
                }
            }
        )
    },
    //delThis 删除某一项的照片
    delThis(index){
        this.pictrueList.splice(index,1)
    }, 
    submit(){
        if(!this.type[0]){
          return this.$vux.toast.show({
                text: '请选择变更类型',
                type:'text'
            })  
        }
        if(!this.stockNum){
            return this.$vux.toast.show({
                text: '请输入损耗数量',
                type:'text'
            })
        }
        if( Number(this.stockNum)  > Number(this.$route.query.num) ){
            return this.$vux.toast.show({
                text: '损耗数量不得超出库存数量',
                type:'text'
            })
        }
        if(!this.reson){
            return this.$vux.toast.show({
                text: '请输入原因',
                type:'text'
            })
        }
        api.stockMng.lossApply({
            stockId:this.$route.query.id,
            lossType:this.type[0],
            lossNum:this.stockNum,
            description:this.reson,
            imgs:this.pictrueList.join(",")
        },
            res=>{
                this.$router.go(-1);
                return this.$vux.toast.show({
                    text: '提交成功',
                    type:'text'
                })
            }
        )

    }
  }
}
</script>
<style lang="less" scoped>
    /deep/.vux-no-group-title{
        margin-top: 0;
    }
    /deep/.weui-cells:after{
        left: 20px;
    }
    input {
        outline: none;
    }
    .ipt{
        border: none;
    }
    .areatext{ 
        height: 1.5rem;
        padding: .15rem;
        line-height: .30rem;
        box-sizing: border-box;
        font-size: .24rem;
    }
    .reson-title{
        font-size: .30rem;
        font-weight: bold;
        padding: 10px 0;
    }
    .reson-box{
        width: 100%;
        position: relative;
        textarea{
            width: 100%;
            outline: 0;
            border: 0;
            background: #ececec;
        }
    }
.wrap-box{
    display: flex;
    flex-wrap: wrap;
    .warp-img{
        width: 80px;
        height: 80px;
    }
    .iconguanbi{
        color: #fbfbfb;
        background: rgba(33,33,33,.7);
        font-size: .36rem;
        top: 2px;
        right: 12px;
        position: absolute;
    }
}
.warp-img-box{
    position: relative;
}
.uploader-box{
    width: 100px;
    height: 100px;
}
.invoice-photo{
    padding: .24rem .30rem;
}
.map-title{
    height: 40px;
    line-height: 40px;
    font-size: 16px;
    font-weight: bold;
}
.invoice-photo-img{
   width: 100%;
   min-width: 74px;
   min-height: 74px; 
}
.tips{
    color: #999;
    font-size: 11px;
    margin-left: 8px;
}

.add-img {
    width: 100%;
    padding: 10px;
}

.add-img p {
    color: #999;
}
.add-image {
    padding-top: 15px; 
    width: 80px; 
    height: 80px;
    border: 1px dashed rgba(0, 0, 0, .2);
}

.add-image .iconxiangji {
    font-size: 24px;
}
.invoice-money{
    color: #999;
    width: 30%;
}
.invoice-timer{
   color: #666; 
   width: 35%;
}
.invoice-active{
    background: #e1e1e1;
}

.wrap-box{
    display: flex;
    flex-wrap: wrap;
    img{
        margin-bottom: 10px;
        margin-right: 10px;
        width: 74px;
        height: 74px;    
    }
}
.height120{
    height: 60px;
}
.btn{
    text-align: center;
    line-height: 50px;
    background: #FABE00;
    color: #fff;
    height: 50px;
    font-size: 16px;
    letter-spacing: 4px;
    position: fixed;
    width: 100%;
    bottom: 0;
    left: 0;
}
</style>